<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AdminMenuSearch;
use App\Models\Posts\WxUserCircle;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Models\Menu;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AdminMenuSearchController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AdminMenuSearch(), function (Grid $grid) {
            $grid->quickSearch(['id', 'parent_id','menu_id','keyword', 'lower'])->placeholder('搜索...')->width(50);

            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('parent_id')->editable();
            $grid->column('menu_id')->editable();
            $grid->column('keyword')->editable();
            $grid->column('lower')->editable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AdminMenuSearch(), function (Show $show) {
            $show->field('id');
            $show->field('parent_id');
            $show->field('menu_id');
            $show->field('keyword');
            $show->field('lower');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdminMenuSearch(), function (Form $form) {
            $form->display('id');
            $form->select('parent_id')->options(Menu::where('parent_id', 0)->pluck('title', 'id'));
            $form->text('menu_id');
            $form->text('keyword');
            $form->text('lower');
            $form->saving(function (Form $form){
                $form->lower = strtolower($form->keyword);
            });

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
